<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
//获取文件地址文件
function getImg($str,$url=null){
    if($url){
        $url = rtrim($url,'/');
        return $url.'/'.$str;
    }else{
        if(is_file(config('FileRoot').$str)){
            $conf = ltrim(config('FileRoot').$str,'.');
            return $conf;
        }
        if(is_file('./static/images/'.$str)){
            return '/static/images/'.$str;
        }
        $conf = ltrim(config('FileRoot').$str,'.');
        return $conf;
    }
}

/**加密后台密码
 * @param $str
 * @return string
 */
function getPwd($str){
    return md5(md5($str));
}

/**查询当前客户端的ip地址
 * @return string
 */
function ip() {
    //strcasecmp 比较两个字符，不区分大小写。返回0，>0，<0。
    if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
        $ip = getenv('HTTP_CLIENT_IP');
    } elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
        $ip = getenv('HTTP_X_FORWARDED_FOR');
    } elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
        $ip = getenv('REMOTE_ADDR');
    } elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    $res =  preg_match ( '/[\d\.]{7,15}/', $ip, $matches ) ? $matches [0] : '';
//    $res = $_SERVER['REMOTE_ADDR'];
    return $res;
    //dump(phpinfo());//所有PHP配置信息
}

/**检测是否登录
 * @return bool|mixed
 */
function isSign(){
    $user = session('user');
    if(!$user){
        return false;
    }else{
        return $user;
    }
}

/**获取字符串月份的开始和结束时间戳
 * @param $str
 * @return array|bool
 */
function strToMonth($str){
    $a=0;
    if($str==date('Y-m',time())){
        $a=1;
    }
    if(is_string($str)){
        $start = strtotime($str);
        $array = explode('-',$str);
        if($array[1]==12){
            $array[1] =1;
            $array[0] = $array[0]+1;
        }else{
            $array[1] = $array[1]+1;
        }
        if($array[1]<10){
            $array[1] = '0'.(int)$array[1];
        }
        $str = $array[0].'-'.$array[1];
        if($a==1){
            $end = time();
        }else{
            $end = strtotime($str)-1;
        }
    }else{
        return false;
    }
    return ['start'=>$start,'end'=>$end];
}

/**获取某天的开始和结束时间戳
 * @param $str
 * @return array|bool
 */
function strToDay($str){
    $a = 0;
    if($str==date('Y-m-d',time())){
        $a=1;
    }
    if(is_string($str)){
        $start = strtotime($str);
        $array = explode('-',$str);
        $days = date('t',strtotime($str));
        if($array[2] == $days){
            if($array[1]==12){
                $array[0] = $array[0]+1;
                $array[1] = 1;
                $array[2] = 1;
            }else{
                $array[1] = $array[1]+1;
                $array[2] = 1;
            }
        }else{
            $array[2] = $array[2]+1;
        }
        $str = $array[0].'-'.($array[1]>=10?$array[1]:'0'.(int)$array[1]).'-'.($array[2]>=10?$array[2]:'0'.(int)$array[2]);
//        return $str;
        if($a==1){
            $end = time();
        }else{
            $end = strtotime($str)-1;
        }
    }else{
        return false;
    }
    return ['start'=>$start,'end'=>$end];
}
//api返回格式
function returns($code=401,$msg='请登录'){
    return ['code'=>$code,'msg'=>$msg];
}

/**单独验证类
 * @param $data:验证的数据
 * @param array $tiaojian:数组形式的验证条件
 * @return bool
 */
function val($data,array $tiaojian){
    $val = \think\Validate::make($tiaojian);
    if(!$val->check($data)){
        return ['code'=>false,'msg'=>$val->getError()];
    }else{
        return ['code'=>true];
    }
}
